using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ReinsPlugin
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        private ReinsData reinsData;
        public ReinsData ReinsData
        {
            get
            {
                return reinsData;
            }
            set
            {
                reinsData = value;
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            UpdateUI(true);
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            UpdateUI(false);
            this.DialogResult = DialogResult.OK;
            Close();
        }

        private void UpdateUI(bool isDisplay)
        {
            if (isDisplay)
            {
                txt_form_name.Text = reinsData.SearchData.FormName;
                Feild feild = reinsData.SearchData.Type;
                txt_type.Text = feild.Name; 
                lbl_type.Text =feild.Title ;
                foreach (string item in feild.Values)
                {
                    lsb_type.Items.Add(item);
                }
                if (lsb_type.Items.Count > feild.Index)
                {
                    lsb_type.SelectedIndex = feild.Index;
                }

                //feild = reinsData.SearchData.Address1;
                //txt_address1.Text =feild.Name ;
                //lbl_address1.Text = feild.Title;
                //foreach (string item in feild.Values)
                //{
                //    lsb_address1.Items.Add(item);
                //}
                //if (lsb_address1.Items.Count > feild.Index)
                //{
                //    lsb_address1.SelectedIndex = feild.Index;
                //}

                //feild = reinsData.SearchData.Address2;
                //txt_address2.Text = feild.Name;
                //lbl_address2.Text = feild.Title;
                //foreach (string item in feild.Values)
                //{
                //    lsb_address2.Items.Add(item);
                //}
                //if (lsb_address2.Items.Count > feild.Index)
                //{
                //    lsb_address2.SelectedIndex = feild.Index;
                //}
                Feild station_line = reinsData.SearchData.StationLine;
                Feild station_from = reinsData.SearchData.StationFrom;
                Feild station_to = reinsData.SearchData.StationTo;

                txt_station_line.Text = station_line.Name;
                lbl_station_line.Text = station_line.Title;

                txt_station_from.Text = station_from.Name;
                lbl_station_from.Text = station_from.Title;

                txt_station_to.Text = station_to.Name;
                lbl_station_to.Text = station_to.Title;

                lsv_station.Columns.Add(station_line.Title, 50, HorizontalAlignment.Left);
                lsv_station.Columns.Add(station_from.Title, 50, HorizontalAlignment.Left);
                lsv_station.Columns.Add(station_to.Title, 50, HorizontalAlignment.Left);
                for (int i = 0; i < station_line.Values.Count; i++)
                {
                    ListViewItem li = lsv_station.Items.Add((String)station_line.Values[i]);
                    li.SubItems.Add((String)station_from.Values[i]);
                    li.SubItems.Add((String)station_to.Values[i]);
                }
                if (lsv_station.Items.Count > station_line.Index)
                {
                    lsv_station.Items[station_line.Index].Selected = true;
                    lsv_station.Items[station_line.Index].BackColor = Color.Blue;
                }
                foreach (ColumnHeader ch in lsv_station.Columns)
                {
                    ch.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
                }

                Feild price_from = reinsData.SearchData.PriceFrom;
                Feild price_to = reinsData.SearchData.PriceTo;

                txt_price_from.Text = price_from.Name;
                lbl_price_from.Text = price_from.Title;

                txt_price_to.Text = price_to.Name;
                lbl_price_to.Text = price_to.Title;

                lsv_price.Columns.Add(price_from.Title, 50, HorizontalAlignment.Left);
                lsv_price.Columns.Add(price_to.Title, 50, HorizontalAlignment.Left);
                for (int i = 0; i < price_from.Values.Count; i++)
                {
                    ListViewItem li = lsv_price.Items.Add((String)price_from.Values[i]);
                    li.SubItems.Add((String)price_to.Values[i]);
                }
                if (lsv_price.Items.Count > price_from.Index)
                {
                    lsv_price.Items[price_from.Index].Selected = true;
                    lsv_price.Items[price_from.Index].BackColor = Color.Blue;
                }
                foreach (ColumnHeader ch in lsv_price.Columns)
                {
                    ch.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
                }
            }
            else
            {
                reinsData.SearchData.FormName =txt_form_name.Text;
                reinsData.SearchData.Type.Name = txt_type.Text;
                reinsData.SearchData.Type.Title = lbl_type.Text;
                reinsData.SearchData.Type.Values.Clear();
                foreach (object obj in lsb_type.Items)
                {
                    if (obj.GetType() == "".GetType())
                    {
                        reinsData.SearchData.Type.Values.Add(obj);
                    }
                }
                if (lsb_type.SelectedIndex != -1)
                {
                    reinsData.SearchData.Type.Index = lsb_type.SelectedIndex;
                }

                //reinsData.SearchData.Address1.Name = txt_address1.Text;
                //reinsData.SearchData.Address1.Title = lbl_address1.Text;
                //reinsData.SearchData.Address1.Values.Clear();
                //foreach (object obj in lsb_address1.Items)
                //{
                //    if (obj.GetType() == "".GetType())
                //    {
                //        reinsData.SearchData.Address1.Values.Add(obj);
                //    }
                //}
                //if (lsb_address1.SelectedIndex != -1)
                //{
                //    reinsData.SearchData.Address1.Index = lsb_address1.SelectedIndex;
                //}

                //reinsData.SearchData.Address2.Name = txt_address2.Text;
                //reinsData.SearchData.Address2.Title = lbl_address2.Text;
                //reinsData.SearchData.Address2.Values.Clear();
                //foreach (object obj in lsb_address2.Items)
                //{
                //    if (obj.GetType() == "".GetType())
                //    {
                //        reinsData.SearchData.Address2.Values.Add(obj);
                //    }
                //}
                //if (lsb_address2.SelectedIndex != -1)
                //{
                //    reinsData.SearchData.Address2.Index = lsb_address2.SelectedIndex;
                //}

                reinsData.SearchData.StationLine.Name = txt_station_line.Text;
                reinsData.SearchData.StationLine.Title = lbl_station_line.Text;
                reinsData.SearchData.StationFrom.Name = txt_station_from.Text;
                reinsData.SearchData.StationFrom.Title = lbl_station_from.Text;
                reinsData.SearchData.StationTo.Name = txt_station_to.Text;
                reinsData.SearchData.StationTo.Title = lbl_station_to.Text;

                if (lsv_station.SelectedItems.Count > 0)
                {
                    reinsData.SearchData.StationLine.Index = lsv_station.SelectedItems[0].Index;
                    reinsData.SearchData.StationFrom.Index = lsv_station.SelectedItems[0].Index;
                    reinsData.SearchData.StationTo.Index = lsv_station.SelectedItems[0].Index;
                }

                reinsData.SearchData.PriceFrom.Name = txt_price_from.Text;
                reinsData.SearchData.PriceFrom.Title = lbl_price_from.Text;
                reinsData.SearchData.PriceTo.Name = txt_price_to.Text;
                reinsData.SearchData.PriceTo.Title = lbl_price_to.Text;

                if (lsv_price.SelectedItems.Count > 0)
                {
                    reinsData.SearchData.PriceFrom.Index = lsv_price.SelectedItems[0].Index;
                    reinsData.SearchData.PriceTo.Index = lsv_price.SelectedItems[0].Index;
                }
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void lsv_price_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (e.IsSelected)
            {
                e.Item.BackColor = Color.Blue;
            }
            else
            { 
                e.Item.BackColor = lsv_price.BackColor;
            }
        }
    }
}